package com.example.demo.service.impl;

import com.example.demo.mapper.DeptMapper;
import com.example.demo.mapper.MemberMapper;
import com.example.demo.pojo.DeptExample;
import com.example.demo.pojo.Member;
import com.example.demo.pojo.MemberExample;
import com.example.demo.service.MemberService;
import com.example.demo.utils.SearchDo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private DeptMapper deptMapper;


    @Override
    public List<Member> queryMemberById() {
        MemberExample example = new MemberExample();
        example.createCriteria().andIdEqualTo(new BigDecimal(1385));
        return memberMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Member> queryMemberByPage(Integer page, Integer pageSize, SearchDo searchDo) {
        //为了程序的严谨性，判断非空：
        if(page == null){
            //设置默认当前页
            page = 1;
        }
        if(page <= 0){
            page = 1;
        }
        if(pageSize == null){
            //前台默认设置分页显示条数30
            pageSize = 30;
        }
        //1、设置分页信息，包括当前页数和每页显示的总计数
        PageHelper.startPage(page, pageSize);
        //2、执行查询
        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        //2.1 根据查询条件设置查询
        if (searchDo.getDeptName() != null && !"".equals(searchDo.getDeptName())) {
            List<BigDecimal> list = deptMapper.selectIdListByExample(searchDo.getDeptName());
            criteria.andDeptidIn(list);
        }
        if (searchDo.getRealName() != null && !"".equals(searchDo.getRealName())) {
            criteria.andRealnameLike("%"+searchDo.getRealName()+"%");
        }
        if (searchDo.getUsername() != null && !"".equals(searchDo.getUsername())) {
            criteria.andUsernameLike("%"+searchDo.getUsername()+"%");
        }
        if (searchDo.getYgCardNo() != null && !"".equals(searchDo.getYgCardNo())) {
            criteria.andYgcardnoLike("%"+searchDo.getYgCardNo()+"%");
        }
        //2.2 根据查询条件查询oracle
        List<Member> list = memberMapper.selectByExample(example);

        //3、获取分页查询后的数据
        return new PageInfo<>(list);
    }
}
